import request from '../request'
import type {
  CreateOrUpdateUserData,
  GetUsersResponseData,
  CreateOrUpdateResponseData,
  DeleteUserResponseData
} from './type'

enum API {
  GETUSERS_URL = '/user', // 获取user表格接口
  UPLOAD_URL = '/user/upload', // 头像上传接口
  CREATE_URL = '/user', // 新增user接口
  UPDATE_URL = '/user', // 更新user接口
  DELETE_URL = '/user' // 删除user接口
}

// 获取user表格数据接口
export const reqGetUsers = (
  params?: Partial<{
    pageNum: number
    pageSize: number
    keyword: string
  }>
) =>
  request<GetUsersResponseData, any>({
    url: API.GETUSERS_URL,
    method: 'get',
    params
  })

// 头像上传接口
export const uploadAvatarUrl =
  `${import.meta.env.VITE_APP_API_URL}:${import.meta.env.VITE_APP_API_PORT}` +
  import.meta.env.VITE_APP_PATH +
  API.UPLOAD_URL

// 新增用户接口
export const reqCreateUser = (data: CreateOrUpdateUserData) => {
  return request<CreateOrUpdateResponseData, any>({
    url: API.CREATE_URL,
    method: 'post',
    data
  })
}

// 更新用户接口
export const reqUpdateUser = (data: CreateOrUpdateUserData) => {
  return request<CreateOrUpdateResponseData, any>({
    url: API.UPDATE_URL,
    method: 'patch',
    data
  })
}

// 删除用户接口
export const reqDeleteUser = (id: string) => {
  return request<DeleteUserResponseData, any>({
    url: API.DELETE_URL,
    method: 'delete',
    params: { id }
  })
}
